namespace Baddies
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Baddies.Events;
    using Baddies.GUI;
    using Baddies.InputDevices; 
    using Baddies.Nodes;
    using Baddies.Scenes; 
    using Baddies.Utils;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Media;
                 
    /// <summary>
    /// This is the main type for your game.
    /// </summary>
    public class CoreGame : Microsoft.Xna.Framework.Game
    {
        /// <summary>
        /// Graphics device manager.
        /// </summary>
        protected GraphicsDeviceManager graphics;

        /// <summary>
        /// Sprite batch.
        /// </summary>
        protected SpriteBatch spriteBatch;

        /// <summary>
        /// Node for the game tree.
        /// </summary>
        protected Node root;

        /// <summary>
        /// Initializes a new instance of the CoreGame class.
        /// </summary>
        public CoreGame()
        {
            this.graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

            EventManager.GetEV.RegisterNewEvent("GetGameServices", true);
            EventManager.GetEV.Register("GetGameServices", new EventHandler(this.GetGameServices));

            EventManager.GetEV.RegisterNewEvent("GetGameGfxDevice", true);
            EventManager.GetEV.Register("GetGameGfxDevice", new EventHandler(this.GetGameGfxDevice));

            EventManager.GetEV.RegisterNewEvent("PrintTree", false);
            EventManager.GetEV.Register("PrintTree", this.PrintTree);                                 
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            this.spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {            
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            base.Draw(gameTime);           
        }

        /// <summary>
        /// Delegate function for the GetGameServices event.
        /// </summary>
        /// <param name="sender">Sender of the request.</param>
        /// <param name="e">GetGameServicesArg parameter.</param>
        private void GetGameServices(object sender, EventArgs e)
        {
            ((GetGameServicesArg)e).Service = this.Services;
        }

        /// <summary>
        /// Delegate function for the GetGameGfxDevice event.
        /// </summary>
        /// <param name="sender">Sender of the request.</param>
        /// <param name="e">GetGameGfxDeviceArg parameter.</param>
        private void GetGameGfxDevice(object sender, EventArgs e)
        {
            ((GetGameGfxDeviceArg)e).GraphicsDevice = this.GraphicsDevice;
        }

        /// <summary>
        /// Delegate function for the PrintTree event.
        /// </summary>
        /// <param name="sender">Sender of the request.</param>
        /// <param name="e">No EventArgs parameter.</param>
        private void PrintTree(object sender, EventArgs e)
        {
            Log.F.Write(this.root.PrintSubtree());
        }
    }
}
